package shop.local.ui.gui;

import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import shop.local.domain.ShopVerwaltung;
import shop.local.valueobjects.ArtikelListe;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Bestandshistorie extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int maximalBestand;
	private int[] alleBestaende;
	private JFormattedTextField frmtdtxtfldArtnr;
	private JFormattedTextField frmtdtxtfldAnztage;
	private JPanel panel;
	protected int artikelNummer;
	/**
	 * Create the panel.
	 */
	public Bestandshistorie(final ShopVerwaltung shop) {
		setLayout(new MigLayout("", "[grow]", "[][grow]"));
		
		
		ArtikelListe liste = shop.gibAlleArtikel();
		Integer[] artikel = new Integer[liste.size()];
		
		for(int i = 1; i <= liste.size(); i++) {
			artikel[i - 1] = liste.get(i).getNummer();
		}
		
		final JComboBox comboBox = new JComboBox(artikel);		
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				artikelNummer = (Integer) arg0.getItem();
			}
		});

		add(comboBox, "flowx,cell 0 0");	
		
		JLabel lblAnzahlTage = new JLabel("Anzahl Tage:");
		add(lblAnzahlTage, "cell 0 0");
				
		JButton btnAnzeigen = new JButton("Anzeigen");
		btnAnzeigen.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent m) {
				
				if(artikelNummer == 0) {
					// Hole ersten Artikel, wenn keine Auswahl getroffen wurde
					artikelNummer = (Integer) comboBox.getItemAt(0);
				}
				
				alleBestaende = shop.gibHistorie(artikelNummer, (Integer) frmtdtxtfldAnztage.getValue());
				maxBestandErrechnen(alleBestaende);
				if (panel != null) {
					remove(panel);
				}
				panel = new JPanel() {

					private static final long serialVersionUID = 1L;
					
					public void paintComponent(Graphics g) {
						super.paintComponent(g);
				
						int anzTage = alleBestaende.length;
						int abstand = (500)/anzTage;					
						int xWert = abstand; 
						int[] xKoords = new int[alleBestaende.length];
						for (int i = 0; i < anzTage; i++) {
							xKoords[i] = xWert;
							xWert = xWert + abstand;		
						}
						
						int ursprung = maximalBestand + abstand;
						int[] yKoords = new int[alleBestaende.length];
						for (int i = 0; i < yKoords.length; i++) {
							yKoords[i] = ursprung - alleBestaende[i];
						}
						
						Graphics2D flaeche = (Graphics2D) g;
						flaeche.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
										
						flaeche.setColor(Color.BLACK);
						flaeche.setStroke(new BasicStroke(1));
						flaeche.drawLine(25, abstand - 10, 25, ursprung);
						flaeche.drawLine(15, ursprung, xWert, ursprung);
						

						for (int i = 0; i < anzTage; i++) {
							//flaeche.drawString("" + alleBestaende[i], xKoords[i], yKoords[i]);
							if (i == anzTage - 1) {
								flaeche.drawString("Jetzt", xKoords[i], ursprung + 15);
							} else {
								flaeche.drawString(Integer.toString((i + 1)), xKoords[i], ursprung + 15);
							}
						}
						
						int yBes = 25;
						do {
							flaeche.drawString(Integer.toString(yBes), 0, ursprung - yBes + 5);
							flaeche.drawLine(25, ursprung - yBes, 15, ursprung - yBes);
							yBes = yBes + 25;
						} while (yBes <= maximalBestand);
						
						flaeche.setColor(Color.GREEN);
						flaeche.setStroke(new BasicStroke(2));//, BasicStroke.CAP_BUTT ,BasicStroke.JOIN_MITER));
						flaeche.drawPolyline(xKoords, yKoords, anzTage);
						
						int panelBreite = this.getPreferredSize().width;
						int panelHoehe = this.getPreferredSize().height;
						if (ursprung > this.getHeight()) {
							panelHoehe= ursprung + abstand;
						}
						if (xWert > this.getWidth()) {
							panelBreite = xWert + abstand;
						}
						this.setPreferredSize(new Dimension(panelBreite, panelHoehe));
						this.revalidate();
					}	
				};
				add(panel, "cell 0 1,grow");
				validate();
				repaint();
			}
		});
		
		frmtdtxtfldAnztage = new JFormattedTextField();
		frmtdtxtfldAnztage.setColumns(5);
		frmtdtxtfldAnztage.setValue(new Integer(30));
		add(frmtdtxtfldAnztage, "cell 0 0");
		add(btnAnzeigen, "cell 0 0");
		
	}
	
	public void maxBestandErrechnen(int[] bestaende) {
		maximalBestand = 0;
		for (int i = 0; i < bestaende.length; i++) {
			if (bestaende[i] > maximalBestand) {
				maximalBestand = bestaende[i];
			}
		}
	}
}
